package com.infra.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.infra.server.entity.SysRole;
import com.infra.server.entity.SysUserAuth;
import com.infra.server.utils.ConstantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author win
 * @date 2021/11/12 16:22
 * 异步任务
 */
@Slf4j
@Component
public class AsyncTask {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUserAuthService sysUserAuthService;

    /**
     * 修改用户角色类型
     */
    @Async
    public void updateUserType(Long userId, List<Integer> roleIds) {
        // 普通用户
        int userType = 1;
        if (ObjectUtil.isNotEmpty(roleIds)) {
            List<SysRole> roleList = sysRoleService.listByIds(roleIds);
            if (ObjectUtil.isNotEmpty(roleList)) {
                // 刷选出是否时管理员
                for (SysRole s : roleList) {
                    if (s.getName().equals(ConstantUtil.ADMINISTRATOR_ROLE)) {
                        userType = 0;
                        break;
                    }
                }
            }
        }
        UpdateWrapper<SysUserAuth> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId).set("user_type",userType);
        sysUserAuthService.update(updateWrapper);
    }
}
